local addonName = "PSLibBar";
local moduleName = "General";
local Addon = LibStub("AceAddon-3.0"):GetAddon(addonName);
local Module = Addon:NewModule(moduleName);
local L = LibStub("AceLocale-3.0"):GetLocale(addonName);

local LibDBIcon = LibStub("LibDBIcon-1.0");
local LibDataBroker = LibStub("LibDataBroker-1.1");


local sMinimapButton = "General.Settings.MinimapButton";

Module.defaults =
{
	["General.Settings.MinimapButton"] = true,
}

Module.optionTable = 
{
	order = 1.0,
	name = L["General.Settings.Title"],
	type = "group",
	handler = Module, get = "GetValue", set = "SetValue",
	args = 
	{ 
		["General.Settings.MinimapButton"] = 
		{
			name = L["General.Settings.MinimapButton"],
			type = "toggle",
		},
	}
}


local function CreateMinimapIcon(handle,onClickHandler,onTooltipShowhandler)
	local libDBIconMetatable = {
		type = "launcher",
		icon = "Interface\\Icons\\INV_Ingot_Eternium",
		tocname = addonName,
		label = "PSLib bar",
		OnClick = handle[onClickHandler],
		OnTooltipShow = handle[onTooltipShowhandler]
	}
	return libDBIconMetatable;
end

function Module:OnInitialize()
	
end

function Module:OnEnable()
	
end


function Module:OnSettingsChanged(settings)
	for key, value in pairs(settings) do
		if(key == "General.Settings.MinimapButton") then self:OnMinimapButtonSettingChanged(value) end
	end
end

function Module:OnMinimapButtonSettingChanged(newValue)	
	if(newValue) then 
		if(not LibDBIcon:IsRegistered(addonName)) then
			local libDBIconMetatable = CreateMinimapIcon(Module,"OnMinimapButtonClick","OnMinimapTooltip");
			local libDBIconObject = LibDataBroker:NewDataObject(addonName,libDBIconMetatable);		
			LibDBIcon:Register(addonName, libDBIconObject);
		end
		
		LibDBIcon:Show(addonName);
	else 
		if(LibDBIcon:IsRegistered(addonName)) then LibDBIcon:Hide(addonName); end
	end
	
end

function Module:OnMinimapButtonClick(clickedframe, button)
	Addon:ShowConfigDialog();
end

function Module:OnMinimapTooltip(tooltip)	
end